package ForkJoin;

import org.junit.Test;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;
import java.util.stream.LongStream;

/**
 * 提供序列号
 */
public class TestForkJoin {
    @Test
    public void test() {
        Instant start = Instant.now();//计算时间戳
        ForkJoinPool pool = new ForkJoinPool();
        ForkJoinTask<Long> task = new ForkJoin(0, 1000000000);
        Long invoke = pool.invoke(task);
        Instant end = Instant.now();
        System.out.println(invoke);
        //Duration.between(start,end).toMillis();计算时间戳
        System.out.println(Duration.between(start, end).toMillis());
    }

    @Test
    public void test2() {
        Instant start = Instant.now();
        long sum = 0L;
        for (long i = 0; i <= 1000000000L; i++) {
            sum += i;

        }
        System.out.println(sum);
        Instant end = Instant.now();
        System.out.println(Duration.between(start, end).toMillis());

    }

    @Test
    public void test3() {
        Instant start = Instant.now();
        System.out.println(LongStream.rangeClosed(0, 100000000000L).parallel().reduce(0, Long::sum));
        Instant end = Instant.now();
        System.out.println(Duration.between(start, end).toMillis());
    }
}
